package cz.muni.fi.pa165.hauntedhouses.web;

import cz.muni.fi.pa165.hauntedhouses.dto.AbilityDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.GhostDTO;
import cz.muni.fi.pa165.hauntedhouses.facade.ServiceFacade;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@UrlBinding("/jsp/admin/abilities")
public class AbilityActionBean implements ActionBean {
 
    final static Logger log = LoggerFactory.getLogger(AbilityActionBean.class);
 
    private ActionBeanContext context;
 
    @SpringBean
    protected ServiceFacade facade;
 
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/WEB-INF/jsp/admin/abilities.jsp");
    }
 
    public List<AbilityDTO> getAbilities() {
        return facade.findAllAbilities();
    }
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"add", "save"}, field = "name", required = true),
            @Validate(on = {"add", "save"}, field = "description", required = true),
            @Validate(on = {"add", "save"}, field = "damage", required = true, minvalue = 1),
            @Validate(on = {"add", "save"}, field = "abilityLevel", required = true, minvalue = 1, maxvalue=3),
            @Validate(on = {"add", "save"}, field = "abilityType", required = true)
    })
    private AbilityDTO ability;
 
    public Resolution add() {
        log.debug("add() ability={}", ability);
        facade.createAbility(ability);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public AbilityDTO getAbility() {
        return ability;
    }
 
    public void setAbility(AbilityDTO ability) {
        this.ability = ability;
    }
    
     public Resolution delete() {
        log.debug("delete({})", ability.getId());
        for(GhostDTO ghost: facade.findAllGhosts()){
            ghost.getAbilities().remove(ability);
            facade.updateGhost(ghost);
        }
        facade.removeAbility(ability);
        return new RedirectResolution(this.getClass(), "all");
    }
     
     @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save","delete"})
    public void loadAbilityFromDatabase() {
        String ids = context.getRequest().getParameter("ability.id");
        if (ids == null) {
            return;
        }
        ability = facade.getAbility(Long.parseLong(ids));
    }
 
    public Resolution edit() {
        log.debug("edit() ability={}", ability);
        return new ForwardResolution("/WEB-INF/jsp/admin/abilityEdit.jsp");
    }
 
    public Resolution save() {
        log.debug("save() ability={}", ability);
        facade.updateAbility(ability);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution storno() {
        log.debug("storno() ability={}", ability);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
 
    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
}